import { adminSupply } from '@/api/content.js'

export let adSupply = {
    namespaced: true,
    state: ()=>({
        supplies: [],
        editSupply: {
            id: '',
            name: '',
            type: '',
            size: '',
            origin: '',
            trad_place: '',
            add_time: '',
            update_time: '',
            price: '',
            rec: '',
            detail_text: '',
            img_list: '',
            thumb: '',
        },
        editID: -1,
        count: -1,
        currentTabs: 'data',
        loading: false,
        showEdit: false,
    }),
    mutations: {
        async getSupplies(state, page) {
            if (state.loading)return
            state.loading = true
            let data = await adminSupply.getAll(page);
            state.loading = false
            state.supplies = data.supply
            state.count = data.count
        },
        async setEditSupply(state, scope){
            state.editID = scope.index_;
            state.loading = true;
            let data = await adminSupply.getSupply(scope.row.id);
            state.editSupply = data.supply
            state.loading = false;
            state.showEdit = true;
            state.currentTabs = 'edit';
        },
        changeSupply(state, supply){
            state.supplies[state.editID] = supply
            state.showEdit = false
            state.currentTabs = 'data'
        },
        unshiftSupply(state, supply){
            state.supplies.unshift(supply)
        },
        spliceSupply(state, index){
            state.supplies.splice(index, 1)
        },
        cancelEdit(state){
            state.editSupply = {
                id: '',
                name: '',
                type: '',
                size: '',
                origin: '',
                trad_place: '',
                add_time: '',
                update_time: '',
                price: '',
                rec: '',
                detail_text: '',
                img_list: '',
                thumb: '',
            }
            state.currentTabs = 'data';
            state.showEdit = false ;
        },
        setLoading(state, flag){
            state.loading = flag
        }
        
    }
}